#include "server.h"

void Server::start_accept()
{
    auto session = std::make_shared<Session>(_ioc, this);
    _acceptor.async_accept(session->socket(),
        [this, session](const boost::system::error_code& error) {
            handle_accept(error, session);
        }
    );
}

void Server::delete_session(std::string id)
{
    auto it = _sessions.find(id);
    if (it != _sessions.end())
    {
        _sessions.erase(it);
    }
}

void Server::handle_accept(const boost::system::error_code& error, std::shared_ptr<Session> session)
{
    if (error)
    {
        std::cout << "Error in accept: " << error.message() << std::endl;
        start_accept();
        return;
    }

    session->start();
    _sessions.insert(make_pair(session->uuid(), session));

    start_accept();
}